﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using DTO;
using UTIL;

namespace DAO
{
    public class NhanXetDao
    {
        public static bool ThemNhanXet(NhanXetDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_themNhanXet");
                StoreUtilities.AddInputPara(cmd, "@IDNguoidung", SqlDbType.Int, one.IDNguoiDung);
                StoreUtilities.AddInputPara(cmd, "@MaSanPham", SqlDbType.Int, one.MaSanPham);
                StoreUtilities.AddInputPara(cmd, "@NoiDung", SqlDbType.NVarChar, one.NoiDung);
                StoreUtilities.AddInputPara(cmd, "@ThoiDiem", SqlDbType.DateTime, one.ThoiDiem);
                StoreUtilities.AddInputPara(cmd, "@TenNguoiDung", SqlDbType.VarChar, one.TenNguoiDung);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }
        
        public static bool xoaNhanXet(NhanXetDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_xoaNhanXet");
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }

        public static NhanXetDto LayNhanXetTheoID(int ID)
        {
            // ket noi CSDL
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layNhanXet");
            StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, ID);

            SqlDataReader dr = cmd.ExecuteReader();
            NhanXetDto nhanxet = null;
            while (dr.Read())
            {
                nhanxet = new NhanXetDto();
                nhanxet.ID = (int)dr["ID"];
                nhanxet.IDNguoiDung = (int)dr["IDNguoiDung"];
                nhanxet.MaSanPham = (int)dr["MaSanPham"];
                nhanxet.NoiDung = (String)dr["NoiDung"];
                nhanxet.ThoiDiem = (DateTime)dr["ThoiDiem"];
                nhanxet.TenNguoiDung = (String)dr["TenNguoiDung"];
            }
            // dong ket noi
            cnn.Close();

            return nhanxet;
        }

        public static List<NhanXetDto> LayNhanXetCuaSanPham(int IDsanpham)
        {
            // ket noi CSDL
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layNhanXetSanPham");
            StoreUtilities.AddInputPara(cmd, "@IDsanpham", SqlDbType.Int, IDsanpham);

            SqlDataReader dr = cmd.ExecuteReader();

            List<NhanXetDto> danhsach = new List<NhanXetDto>();
            NhanXetDto nhanxet = null;
            while (dr.Read())
            {
                nhanxet = new NhanXetDto();
                nhanxet.ID = (int)dr["ID"];
                nhanxet.IDNguoiDung = (int)dr["IDNguoiDung"];
                nhanxet.MaSanPham = (int)dr["MaSanPham"];
                nhanxet.NoiDung = (String)dr["NoiDung"];
                nhanxet.ThoiDiem = (DateTime)dr["ThoiDiem"];
                nhanxet.TenNguoiDung = (String)dr["TenNguoiDung"];

                danhsach.Add(nhanxet);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;
        }

        public static List<NhanXetDto> LayNhanXetTheoNguoiDung(int IDnguoidung)
        {
            // ket noi CSDL
            SqlConnection cnn = DataConnector.MakeConnection();
            String sql = "select * from nhanxet where IDnguoidung = " + IDnguoidung;
            SqlCommand cmd = new SqlCommand(sql, cnn);

            SqlDataReader dr = cmd.ExecuteReader();

            List<NhanXetDto> danhsach = new List<NhanXetDto>();
            NhanXetDto nhanxet = null;
            while (dr.Read())
            {
                nhanxet = new NhanXetDto();
                nhanxet.ID = (int)dr["ID"];
                nhanxet.IDNguoiDung = (int)dr["IDNguoiDung"];
                nhanxet.MaSanPham = (int)dr["MaSanPham"];
                nhanxet.NoiDung = (String)dr["NoiDung"];
                nhanxet.ThoiDiem = (DateTime)dr["ThoiDiem"];
                nhanxet.TenNguoiDung = (String)dr["TenNguoiDung"];

                danhsach.Add(nhanxet);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;
        }
        public static bool KiemtraTonTai(NhanXetDto one)
        {
            bool result = false;
            NhanXetDto temp = LayNhanXetTheoID(one.ID);
            if (temp != null)
                result = true;

            return result;
        }

        public static int XoaNhanXetTheoID(int id)
        {
            SqlConnection cnn = DataConnector.MakeConnection();
            string sql = "delete from NhanXet where id=" + id;
            SqlCommand cmd = new SqlCommand(sql, cnn);
            int result = cmd.ExecuteNonQuery();
            cnn.Close();

            return result;
        }
    }
}
